package bb.lanxing.util.ui;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ObjectAnimator;
import android.animation.PropertyValuesHolder;
import android.animation.ValueAnimator;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.view.View;
import android.view.animation.DecelerateInterpolator;

public class ItemHighlightAnimator {
    private Animator animator;
    private int backgroundColor;
    private int highlightColor;
    private View itemView;
    private Drawable originalBackground;

    public ItemHighlightAnimator(View item, int highlightColor, int backgroundColor) {
        this.itemView = item;
        this.originalBackground = item.getBackground();
        this.highlightColor = highlightColor;
        this.backgroundColor = backgroundColor;
    }

    public void start() {
        if (Build.VERSION.SDK_INT > 21) {
            View view = this.itemView;
            int i = this.backgroundColor;
            this.animator = ObjectAnimator.ofArgb(view, "backgroundColor", i, this.highlightColor, i);
        } else {
            int i2 = this.backgroundColor;
            int i3 = (i2 >> 24) & 255;
            int i4 = (i2 >> 16) & 255;
            int i5 = (i2 >> 8) & 255;
            int i6 = i2 & 255;
            int i7 = this.highlightColor;
            ValueAnimator.ofPropertyValuesHolder(PropertyValuesHolder.ofInt("alpha", i3, (i7 >> 24) & 255, i3), PropertyValuesHolder.ofInt("red", i4, (i7 >> 16) & 255, i4), PropertyValuesHolder.ofInt("green", i5, (i7 >> 8) & 255, i5), PropertyValuesHolder.ofInt("blue", i6, i7 & 255, i6)).addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                @Override
                public void onAnimationUpdate(ValueAnimator animation) {
                    ItemHighlightAnimator.this.itemView.setBackgroundColor(Color.argb(((Integer) animation.getAnimatedValue("alpha")).intValue(), ((Integer) animation.getAnimatedValue("red")).intValue(), (Integer) animation.getAnimatedValue("green"), (Integer) animation.getAnimatedValue("blue")));
                }
            });
        }
        this.animator.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
                ItemHighlightAnimator.this.resumeBackground();
            }

            @Override
            public void onAnimationCancel(Animator animation) {
                super.onAnimationCancel(animation);
                ItemHighlightAnimator.this.resumeBackground();
            }
        });
        this.animator.setInterpolator(new DecelerateInterpolator());
        this.animator.setDuration(800L);
        this.animator.start();
    }

    public void cancel() {
        Animator animator = this.animator;
        if (animator == null || !animator.isRunning()) {
            return;
        }
        this.animator.cancel();
    }

    public void resumeBackground() {
        if (Build.VERSION.SDK_INT < 16) {
            this.itemView.setBackgroundDrawable(this.originalBackground);
        } else {
            this.itemView.setBackground(this.originalBackground);
        }
    }
}
